{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eab70a82",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Execute this cell to install dependencies\n",
    "%pip install sf-hamilton[visualization]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40ebfe63",
   "metadata": {},
   "source": [
    "# Conttribution: Translate to Hamilton [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/dagworks-inc/hamilton/blob/main/examples/contrib/notebooks/dagworks-translate_to_hamilton.ipynb) [![GitHub badge](https://img.shields.io/badge/github-view_source-2b3137?logo=github)](https://github.com/apache/hamilton/blob/main/examples/contrib/notebooks/dagworks-translate_to_hamilton.ipynb)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1d54a5a",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from hamilton import driver, dataflows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad3cea38",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# import module -- pick one\n",
    "# translate_to_hamilton = dataflows.import_module('translate_to_hamilton')\n",
    "# from hamilton.contrib.dagworks import translate_to_hamilton"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c9336522",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-11T04:53:48.501891Z",
     "start_time": "2023-12-11T04:53:48.230975Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# find some code to change.\n",
    "def my_func(b, c):\n",
    "    \"\"\"Pretend this is a big function and we want to conver the body of it to hamilton.\"\"\"\n",
    "    a = b + c\n",
    "    d = some_func(a)\n",
    "    e = d + 1\n",
    "    return e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cceb7a20",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-11T04:56:15.313971Z",
     "start_time": "2023-12-11T04:56:14.930079Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# get the code\n",
    "import inspect\n",
    "user_code = inspect.getsource(my_func)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "537be2ec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-11T04:56:40.058551Z",
     "start_time": "2023-12-11T04:56:39.312301Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'def my_func(b, c):\\n    \"\"\"Pretend this is a big function.\"\"\"\\n    a = b + c\\n    d = some_func(a)\\n    e = d + 1\\n    return e\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b31be190",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-11T04:59:14.902769Z",
     "start_time": "2023-12-11T04:59:14.408612Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Note: Hamilton collects completely anonymous data about usage. This will help us improve Hamilton over time. See https://github.com/apache/hamilton#usage-analytics--data-privacy for details.\n"
     ]
    }
   ],
   "source": [
    "# create a driver\n",
    "dr = (\n",
    "    driver.Builder()\n",
    "        .with_config({})\n",
    "        .with_modules(translate_to_hamilton)\n",
    "        .build()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9492e7b9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-11T04:59:55.748817Z",
     "start_time": "2023-12-11T04:59:19.193779Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "result = dr.execute(\n",
    "    [\"code_segments\", \"translated_code_response\"], # request these as outputs\n",
    "    inputs={\"user_code\": user_code, \"model_name\": \"gpt-4-1106-preview\"}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "13bbb56e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-11T05:01:49.575797Z",
     "start_time": "2023-12-11T05:01:49.570792Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# functions.py\n",
      "def a(b: float, c: float) -> float:\n",
      "    \"\"\"Computes the sum of b and c.\"\"\"\n",
      "    return b + c\n",
      "\n",
      "def d(a: float) -> float:\n",
      "    \"\"\"Applies some_func to a.\"\"\"\n",
      "    return some_func(a)\n",
      "\n",
      "def e(d: float) -> float:\n",
      "    \"\"\"Increments d by 1.\"\"\"\n",
      "    return d + 1\n",
      "\n",
      "# run.py\n",
      "from hamilton import driver\n",
      "import functions\n",
      "\n",
      "# Instantiate a Driver\n",
      "dr = driver.Driver(config={}, module=functions)\n",
      "\n",
      "# Specify the final output we want from the Driver\n",
      "output = [\"e\"]\n",
      "\n",
      "# Suppose we know the values of `b` and `c` we want to use\n",
      "inputs = {\"b\": 1, \"c\": 2}\n",
      "\n",
      "# Execute the data flow to produce the output\n",
      "result = dr.execute(output, inputs=inputs)\n",
      "\n",
      "print(result['e'])  # The result is a dictionary with keys as the output names we specified earlier\n"
     ]
    }
   ],
   "source": [
    "for code in result[\"code_segments\"]:\n",
    "    print(code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7887d073",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-11T05:00:11.575020Z",
     "start_time": "2023-12-11T05:00:11.539851Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In Hamilton, each part of the procedural function `my_func` that assigns a value to a variable can be refactored into its own Hamilton function. Here's how you might represent this code with separate functions:\n",
      "\n",
      "```python\n",
      "# functions.py\n",
      "def a(b: float, c: float) -> float:\n",
      "    \"\"\"Computes the sum of b and c.\"\"\"\n",
      "    return b + c\n",
      "\n",
      "def d(a: float) -> float:\n",
      "    \"\"\"Applies some_func to a.\"\"\"\n",
      "    return some_func(a)\n",
      "\n",
      "def e(d: float) -> float:\n",
      "    \"\"\"Increments d by 1.\"\"\"\n",
      "    return d + 1\n",
      "```\n",
      "\n",
      "In this example, `some_func` is assumed to be an existing function that takes a single argument and returns a single result. In actual implementation, `some_func` would be defined elsewhere, or you would need to create a Hamilton function that correctly replicates its functionality.\n",
      "\n",
      "Now, you'd want to set up the driver script (`run.py`) to execute these functions:\n",
      "\n",
      "```python\n",
      "# run.py\n",
      "from hamilton import driver\n",
      "import functions\n",
      "\n",
      "# Instantiate a Driver\n",
      "dr = driver.Driver(config={}, module=functions)\n",
      "\n",
      "# Specify the final output we want from the Driver\n",
      "output = [\"e\"]\n",
      "\n",
      "# Suppose we know the values of `b` and `c` we want to use\n",
      "inputs = {\"b\": 1, \"c\": 2}\n",
      "\n",
      "# Execute the data flow to produce the output\n",
      "result = dr.execute(output, inputs=inputs)\n",
      "\n",
      "print(result['e'])  # The result is a dictionary with keys as the output names we specified earlier\n",
      "```\n",
      "\n",
      "Please note that we directly put the values for `b` and `c` in the `inputs` dictionary as an example. In an actual Hamilton application, these might come from other data sources or computations within the directed acyclic graph (DAG).\n"
     ]
    }
   ],
   "source": [
    "print(result[\"translated_code_response\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0d23b2d",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
